﻿<?php
	include '../inc/conn.php';
	include '../sendmail.php';
	
	session_start();
	extract($_POST);
	extract($_GET);
	$pagestr = end(explode("/",$_SERVER['PHP_SELF']));
	unset($_POST,$_GET);
	
	if (isset($act))
	{
		if ($act == 'add' && isset($_SESSION['logname']))
		{
			if (empty($sku) || empty($ssid)){
				echo "<script>alert('机种或SSID值不能为空');window.location='$pagestr';</script>";
				die("机种或SSID值不能为空!");
			}
			$sku=trim(strtoupper($sku));
			$ssid=trim(strtoupper($ssid));
			if (strlen($ssid)!=8){
				echo "<script>alert('SSID长度不对！');window.location='$pagestr';</script>";
				die("SSID长度不对!");
			}
			
			if (strlen($sku)!=7){
				echo "<script>alert('SKU长度不对！');window.location='$pagestr';</script>";
				die("SKU长度不对！");
			}
			
			if(isset($isintel))
				$isintel=1;
			else
				$isintel=0;

			$db = new mysql();
			$sql="INSERT INTO `sb_ssid` (`SKU`,`SSID`,`isIntel`) values('${sku}', '${ssid}', $isintel)";
			$db->query($sql);
			//SendMail("增加[$sku] Sound Blaster SSID", "<b>机种：</b><font color='blue'>$sku</font></br><b>SSID：</b><font color='blue'>$ssid</font></br>需要确认是否正确！", "TE");
			$param = array(
				'aSync'=>'Yes',
				'title'=>"增加[$sku] Sound Blaster and THX SSID",
				'msg'=>"<b>机种：</b><font color='blue'>$sku</font></br><b>SSID：</b><font color='blue'>$ssid</font></br>需要确认是否正确！",
				'to'=>'TE'
			);
			doAsyncMail($param);
			$db->close();
			echo "<script>alert('数据添加成功!');window.opener.location.reload();window.location.href='$pagestr'</script>";
		}
		
		if ($act == 'change' && isset($_SESSION['logname']))
		{
			if (empty($sku) || empty($ssid)){
				echo "<script>alert('机种或SSID值不能为空!!');window.location='$pagestr';</script>";
				die("机种或SSID值不能为空!");
			}
			
			$sku=trim(strtoupper($sku));
			$ssid=trim(strtoupper($ssid));
			if (strlen($ssid)!=8){
				echo "<script>alert('SSID长度不对！');window.location='$pagestr';</script>";
				die("SSID长度不对!");
			}

			if (strlen($sku)!=7){
				echo "<script>alert('SKU长度不对！');window.location='$pagestr';</script>";
				die("SKU长度不对！");
			}
			
			if(isset($isintel))
				$isintel=1;
			else
				$isintel=0;
			
			$db = new mysql();
			$table = "sb_ssid";
			$condition = "ID=${id}";
			$mod_content="`SKU`='${sku}', `SSID`='${ssid}', `isIntel`=$isintel";
			$db->update($table, $mod_content, $condition);
			$db->close();
			echo "<script>alert('内容修改完成!');window.location.href='$pagestr';</script>";
			
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script>
	$(function(){
		$("#ADDDIV").hide();
		$("#ADDSSID").click(function(){
			$("#ADDDIV").toggle();
		});

		$("#addbtn").click(function(){
			if ($("#sku").val() == ""){
				alert("机种名没有填写！");
				return false;
			}

			if ($("#ssid").val() == ""){
				alert("SSID没有填写！");
				return false;
			}			
		});
	});
</script>
<title>新增SSID项目</title>
</head>
<body class="background">
<div class="container-fluid white-background pagecontent">
<?
	if(isset($_SESSION['logname'])){
		if ($pagename=="changeitem"){
			echo "<label>操作项次: </label><a id='ADDSSID' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a> | <a id='ADDDUTY' href='ssidList.php'> <span class=\"glyphicon glyphicon-backward\"></span>返回</a>";
		}else{
			echo "<label>操作项次: </label><a id='ADDSSID' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a>";
		}
	}
	echo "<div id='ADDDIV'>\n";
	echo "<form action='$pagestr?act=add' method='post'>\n";
	echo "	<div>\n";
	echo "	添加内容:<br />\n";
	echo "		SKU : <input name='sku' id='sku'><br>\n";
	echo "		SSID: <input name='ssid' id='ssid'><br>\n";
	echo "		Intel: <input type='checkbox' name='isintel' value='1' />\n";
	echo "	</div>\n";
	echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit' id='addbtn'>\n<input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
	echo "</form>\n";
	echo "</div>\n";

	// change item page
	if ($pagename=="changeitem"){
		$db = new mysql();
		$sql="select * from sb_ssid where ID=$id";
		$db->query($sql);
		$num = $db->db_num_rows();
		$row = $db->fetch_assoc();
		echo "\n<form action='$pagestr?act=change&id=${row['ID']}' method='post'>\n";
		echo "	<div>\n";
		echo "	修改内容:<br />\n";
		echo "		SKU :<input name='sku' id='sku' value='${row['SKU']}'><br>";
		echo "		SSID:<input name='ssid' id='ssid' value='${row['SSID']}'><br>";
		if($row['isIntel']==true){
			echo "		Intel: <input type='checkbox' name='isintel' value='1' checked='checked' />";
		}else{
			echo "		Intel: <input type='checkbox' name='isintel' value='1' />";
		}
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit' id='addbtn'>\n";
		echo "  <input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		$db->close();
	}else{
		$db = new mysql();
		if(isset($sortby)){
			switch($sortby){
				case "su":
					$db->query("select * from sb_ssid order by SKU asc");
					break;
				case "sid":
					$db->query("select * from sb_ssid order by SSID asc");
					break;
			}
		}else{
			$db->query("select * from sb_ssid order by up_date desc");
		}
		$num = $db->db_num_rows();
		echo "<table border=0 cellpadding=0 class='table table-striped'>";
		echo "<tr class='success'><th>Item</th><th><a href='$pagestr?sortby=su'>机种</a></th><th><a href='$pagestr?sortby=sid'>SSID</a></th><th>Is Intel</th><th>更新时间</th><th>操作</th></tr>";
		for($i=0; $i<$num; $i++){
			$row = $db->fetch_assoc();
			$item = $i + 1;
			echo "<tr>";
			echo "<td>$item</td>";
			if(substr($row['up_date'],0,10)==date("Y-m-d")){
				echo "<td><font color='blue'>${row['SKU']}</font></td>";
				echo "<td><font color='blue'>${row['SSID']}</font></td>";
			}else{
				echo "<td>${row['SKU']}</td>";
				echo "<td>${row['SSID']}</td>";				
			}
			echo "<td>${row['isIntel']}</td>";
			echo "<td>${row['up_date']}</td>";
			if(isset($_SESSION['logname'])){
				echo "<td><a href='$pagestr?pagename=changeitem&id=${row['ID']}'><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
			}else{
				echo "<td>#</td>";
			}
			echo "</tr>\n";
		}
		echo "</table>";
		$db->close();
	}
?>
</div>
</body>
</html>
